﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace common
{
    public static class FileUtils
    {
        public static ServerConnectionInfo GetConnectionInfo()
        {
            var lines = File.ReadAllLines(@"config.xxx");
            var result = new ServerConnectionInfo { Address = IPAddress.Parse(lines[0]), Ip = lines[0] };
            try
            {
                result.Port = int.Parse(lines[1]);
            }
            catch (Exception)
            {
                result.Port = 9010;
            }
            return result;
        }

        public static List<PlayerInfo> GetPlayer(string fileName)
        {
            var lines = File.ReadAllLines(fileName);
            return lines.Select(line => line.Split('|')).Select(temp => new PlayerInfo
            {
                Ip = temp[0],
                Port = int.Parse(temp[1]),
                Name = temp[2],
                Mark = int.Parse(temp[3])
            }).ToList();
        }

        public static IList<QuestionManagement> LoadQuestionFromFile(string filename)
        {
            IList<QuestionManagement> results = new List<QuestionManagement>();
            try
            {
                var re = File.OpenText(filename);
                string input;
                var correct = 0;
                while ((input = re.ReadLine()) != null)
                {
                    var temp = ParseQuestion(input);
                    if (temp != null) results.Add(temp);
                }
                re.Close();
            }
            catch
            {
                return null;
            }
            return results;
        }

        public static QuestionManagement ParseQuestion(string input)
        {
            if (input.Equals("")) return null;
            int count;
            var index = -1;
            var correct = 0;
            var i = 0;
            var tmpQues = string.Empty;
            var flagQuestion = true; //false when question end.
            var ans = new List<string>();
            if (input[0] == ':' && input[1] == ':')
            {
                count = 2;
                i = 2;
            }
            else
            {
                count = 4;
            }
            for (; i < input.Length; i++)
            {
                if (count != 4)
                {
                    if (input[i] == ':') count++;
                }
                else
                {
                    //Loading Question
                    if (flagQuestion)
                    {
                        if (input[i] == '{') //If question end
                        {
                            flagQuestion = false;
                        }
                        else
                        {
                            tmpQues += input[i];
                        }
                    }
                    else
                    {
                        //Input answers
                        if ((input[i] == '=' && input[i - 1] != '\\')
                            || (input[i] == '~' && input[i - 1] != '\\'))
                        {
                            ans.Add("");
                            index++;
                            if (input[i] == '=')
                            {
                                correct = index;
                            }
                        }
                        else
                        {
                            if (!(input[i] == '}' && input[i] != '\\'))
                            {
                                ans[index] += input[i];
                            }
                        }
                    }
                }
            }
            return new QuestionManagement(tmpQues.Trim(), ans, correct);
        }
    }
}
